/*
 * Copyright (C) 2009 Tom Barber
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
 *
 * @created Apr 23, 2009 
 * @author Tom Barber
 */


package com.encon.projecttracker.client;

import java.util.ArrayList;
import java.util.List;

import com.encon.projecttracker.client.util.factory.ConstantFactory;
import com.encon.projecttracker.client.util.factory.ServiceFactory;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.HeadElement;
import com.google.gwt.dom.client.Node;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 *
 * @author tom(at)wamonline.org.uk
 */

public class Projecttracker implements EntryPoint { 

	/**
     * The {@link Application}.
     */
    private static Application app = new Application();

    private static ArrayList<String> roles;
	/**
     * Global Session ID.
     */
    private static String sessionid;
    
    public static String getSessionID() {
	return sessionid;
    }
    /**
     * Sets the SESSION_ID.
     */
    
    private static void setSessionID() {
	ServiceFactory.getSessionInstance().createSession(
		new AsyncCallback<String>() {

		    public void onFailure(final Throwable arg0) {
			arg0.getLocalizedMessage();
		    }

		    public void onSuccess(final String arg0) {
			sessionid = arg0;
			setUserRoles(sessionid);
		    }

		});

    }

    public static List<String> getUserRoles(){
    	return roles;
    }

    private static void setUserRoles(String sessionId) {
    	ServiceFactory.getSessionInstance().getRole(sessionId,
    		new AsyncCallback() {

    		    public void onFailure(final Throwable arg0) {
    			arg0.getLocalizedMessage();
    		    }

    		    public void onSuccess(final Object arg0) {
    			roles = (ArrayList<String>) arg0;
    			app.setupAdminStuff();
    		    }
    		});

        }
    
    private static void getUserGroup(){
   // 	ServiceFactory.getSessionInstance()
    }
	/**
	 * The current style theme.
	 */
	
	public static String CUR_THEME = ConstantFactory.getInstance().STYLE_THEMES[0];
    
    
    public final void onModuleLoad() {
	  setSessionID();
	  
		// Swap out the style sheets for the RTL versions if needed
		updateStyleSheets();

		setupTitlePanel();
		
		// hide splash
		com.google.gwt.user.client.DOM
			.getElementById("splash").getStyle().setProperty("display", "none"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
/*
	  HorizontalPanel horizPanel = new HorizontalPanel();
	  Button button = new Button("Fire Query");
	  final FlexTable flexTable = new FlexTable();
	  horizPanel.add(button);
	  horizPanel.add(flexTable);
	  button.addClickListener(new ClickListener(){
	      
	    public void onClick(Widget arg0) {
		// TODO Auto-generated method stub
		ServiceFactory.getQueryInstance().getRecords(getSessionID(), new AsyncCallback<List<TestDTO>>(){

			public void onFailure(Throwable arg0) {
				// TODO Auto-generated method stub
				arg0.printStackTrace();
			}

			public void onSuccess(List<TestDTO> arg0) {
				// TODO Auto-generated method stub
				for (int i=0; i < arg0.size(); i++){
				    TestDTO element = arg0.get(i);
				    
				    HTML list = new HTML(element.getColumn1());
				    
				    flexTable.setWidget(i, 0, list);
				}
				
			}

			
		
	});

	    }
	      
	  });
	  
	  RootPanel.get("splash_loading").add(horizPanel);
	*/
    }
	/**
	 *TODO JAVADOC
	 *
	 */
	private void setupTitlePanel() {
		// Get the title from the internationalized constants
		final String pageTitle = "<h1>" + ConstantFactory.getInstance().mainTitle() //$NON-NLS-1$
			+ "</h1><h2>" + ConstantFactory.getInstance().mainSubTitle() //$NON-NLS-1$
			+ "</h2>"; //$NON-NLS-1$

		// Add the title and some images to the title bar
		final HorizontalPanel titlePanel = new HorizontalPanel();
		titlePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		// titlePanel.add(IMAGES.gwtLogo().createImage());
		titlePanel.add(new HTML(pageTitle));
		app.setTitleWidget(titlePanel);

		
	}
	/**
	 * Get the style name of the reference element defined in the current GWT
	 * theme style sheet.
	 *
	 * @param prefix
	 *            the prefix of the reference style name
	 * @return the style name
	 */
	private static String getCurrentReferenceStyleName(final String prefix) {
	String gwtRef = prefix + "-Reference-" + CUR_THEME; //$NON-NLS-1$
	if (LocaleInfo.getCurrentLocale().isRTL()) {
	    gwtRef += "-rtl"; //$NON-NLS-1$
	}
	return gwtRef;
	}
	/**
	 * Update the style sheets to reflect the current theme and direction.
	 */
	public static void updateStyleSheets() {
	// Generate the names of the style sheets to include
	String gwtStyleSheet = "gwt/" + CUR_THEME + "/" + CUR_THEME + ".css"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	String gwtMosStyleSheet = "gwt/" + CUR_THEME + "/Mosaic.css"; //$NON-NLS-1$ //$NON-NLS-2$
	String scStyleSheet = CUR_THEME + "/Showcase.css"; //$NON-NLS-1$
	final String widgetStyleSheet = "/widgets.css"; //$NON-NLS-1$ // NOPMD by bugg on 21/04/09 05:35
	final String halogenStyleSheet = "/halogen.css"; //$NON-NLS-1$ // NOPMD by bugg on 21/04/09 05:35
	if (LocaleInfo.getCurrentLocale().isRTL()) {
	    gwtStyleSheet = gwtStyleSheet.replace(".css", "_rtl.css"); //$NON-NLS-1$ //$NON-NLS-2$
	    gwtMosStyleSheet = gwtMosStyleSheet.replace(".css", //$NON-NLS-1$
		    "_rtl.css"); //$NON-NLS-1$
	    scStyleSheet = scStyleSheet.replace(".css", "_rtl.css"); //$NON-NLS-1$ //$NON-NLS-2$
	}
	
	// Find existing style sheets that need to be removed
	boolean styleSheetsFound = false;
	final HeadElement headElem = StyleSheetLoader.getHeadElement();
	final List<Element> toRemove = new ArrayList<Element>();
	final NodeList<Node> children = headElem.getChildNodes();
	for (int i = 0; i < children.getLength(); i++) {
	    final Node node = children.getItem(i);
	    if (node.getNodeType() == Node.ELEMENT_NODE) {
		final Element elem = Element.as(node);
		if (elem.getTagName().equalsIgnoreCase("link") //$NON-NLS-1$
			&& elem.getPropertyString("rel").equalsIgnoreCase(//$NON-NLS-1$
				"stylesheet")) { //$NON-NLS-1$
		    styleSheetsFound = true;
		    final String href = elem.getPropertyString("href"); //$NON-NLS-1$
		    // If the correct style sheets are already loaded, then we
		    // should have
		    // nothing to remove.
		    if (!href.contains(gwtStyleSheet)
			    && !href.contains(gwtMosStyleSheet)
			    && !href.contains(scStyleSheet)) {
			toRemove.add(elem);
		    }
		}
	    }
	}
	
	// Return if we already have the correct style sheets
	if (styleSheetsFound && toRemove.isEmpty()) {
	    return;
	}
	
	// Detach the app while we manipulate the styles to avoid rendering
	// issues
	RootPanel.get().remove(app);
	
	// Remove the old style sheets
	for (final Element elem : toRemove) {
	    headElem.removeChild(elem);
	}
	
	// Load the GWT theme style sheet
	final String modulePath = GWT.getModuleBaseURL();
	final Command callback = new Command() {
	    public void execute() {
		// Different themes use different background colors for the body
		// element, but IE only changes the background of the visible
		// content
		// on the page instead of changing the background color of the
		// entire
		// page. By changing the display style on the body element, we
		// force
		// IE to redraw the background correctly.
		RootPanel.getBodyElement().getStyle().setProperty("display", //$NON-NLS-1$
			"none"); //$NON-NLS-1$
		RootPanel.getBodyElement().getStyle()
			.setProperty("display", ""); //$NON-NLS-1$ //$NON-NLS-2$
		RootPanel.get().add(app);
	    }
	};
	
	StyleSheetLoader.loadStyleSheet(modulePath + gwtStyleSheet,
		getCurrentReferenceStyleName("gwt"), null); //$NON-NLS-1$
	StyleSheetLoader.loadStyleSheet(modulePath + gwtMosStyleSheet,
		getCurrentReferenceStyleName("mosaic"), null); //$NON-NLS-1$
	// Load the showcase specific style sheet after the GWT & Mosaic theme
	// style
	// sheet so that custom styles supercede the theme styles.
	StyleSheetLoader.loadStyleSheet(modulePath + scStyleSheet,
		getCurrentReferenceStyleName("Application"), callback); //$NON-NLS-1$
	StyleSheetLoader.loadStyleSheet(modulePath + widgetStyleSheet,
		getCurrentReferenceStyleName("widgets"), null); //$NON-NLS-1$
	StyleSheetLoader.loadStyleSheet(modulePath + halogenStyleSheet,
			getCurrentReferenceStyleName("halogen"), null); //$NON-NLS-1$
	
	}
    
}
